type name = string ;;
type age = int ;;
type adress = string ;;
type telephone = int ;;
type contact = (name * age * adress * telephone) ;;
type agenda = contact list ;;

(*3.1*)
let rec exist x liste =
match liste with
   []-> false
  |e::l -> if x = e then 
              true
           else 
                if x>e then 
                     exist x l
                else 
                     false ;;
 

let rec add_list x liste =
match liste with
   [] -> x::[]
  |e::l -> if e=x then 
                liste
           else
                if x>e then 
                     e::add_list x l
                else 
                     x::e::l ;;


let l = [1;2;4;8;50];;


add_list 0 l ;;


let rec remove_list x liste =
match liste with
   [] -> liste
  |e::l -> if x=e then
              l
           else
                e::remove_list x l;;
              
remove_list 50 l;;

(*3.2*)
let rec map f liste =
match liste with
   []->[]
  |e::l -> (f e)::(map f l);;

map (function x -> x + 1) [1;3;5;7;11];;



let rec iter f = function
   []->()
  |e::l -> (f e; iter f l);;

iter (print_int) [1;3;5;7;11];;


let rec for_all f = function
   [] -> true
  |e::l -> (f e)&&(for_all f l);;

for_all (function x -> x = 0) [0;0;0;0;0];;


(*3.3*)
let upper_case c =
if c>='a'&& c<='z' then
    char_of_int (int_of_char c -32)
else 
    c ;;


upper_case 'c';;


let lower_case c =
if c>='A'&& c<='Z' then
    char_of_int (int_of_char c +32)
else
    c;;

lower_case 'C';;


(*3.4*)
let string_of_char c = String.make 1 c;;


 let string_apply f str = 
  let length = (String.length str) - 1 in 
    let rec apply f str length i =
 match i with
       i when i <= length -> (string_of_char (f str.[i]))^(apply f str length (i+1))
     | _ -> ""
    in apply f str length 0;;


let string_uppercase str =
string_apply upper_case str;;

string_uppercase "Hello";;


let string_lowercase str =
string_apply lower_case str;;

string_lowercase "Hello" ;;


let convert_first_maj str = 
  let length = String.length str - 1
    in 
     let rec apply str length i =
       match i with
	    0      ->  (string_of_char ( upper_case (str.[i])))^(apply str length (i+1))
           |i when i<= length -> (string_of_char str.[i])^(apply str length (i+1))
	   |_ -> ""
     in 
	   apply str length 0;;

convert_first_maj "hello";;

(*4*)
let create_contact (contact,a,b,c) =
(convert_first_maj contact,a,b,c);;


let manage_contact f contact agenda = f contact agenda;;


